package com.weilan.ark3.task;

import com.weilan.ark3.common.GlobalParam;
import com.weilan.ark3.controller.*;
import com.weilan.ark3.entity.ConfigDto;
import com.weilan.ark3.read.ReadRtuData;
import com.weilan.ark3.service.ControlRtuService;
import com.weilan.ark3.service.ModbusConfService;
import com.weilan.ark3.sqlite.SQLiteConfigUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
import org.springframework.scheduling.support.PeriodicTrigger;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.sql.SQLException;
import java.util.concurrent.TimeUnit;


@Component
@EnableScheduling
public class ScheduledTask {

    @Autowired
    private ControlRtuService rtuService;

    @Autowired
    private ModbusConfService modbusConfService;

    @Autowired
    private PeakValleyController peakValleyController;

    @Autowired
    private PvController pvController;

    @Autowired
    private BackupController backupController;

    @Autowired
    private SocRegionController socRegionController;

    @Autowired
    private ReadRtuData loadRtuComponent;
    private ThreadPoolTaskScheduler taskScheduler;

    @PostConstruct
    public void init() {
        taskScheduler = new ThreadPoolTaskScheduler();
        taskScheduler.setPoolSize(10);  // 设定合适的线程池大小
        taskScheduler.initialize();
        scheduleTasks();
    }

    private void scheduleTasks() {
        ConfigDto configDto = SQLiteConfigUtils.query();
        int tacticsTime = configDto.getTacticsTime();
        int deleteTime = configDto.getDeleteTime();
        int faultStoreTime = configDto.getFaultStoreTime();
        int emsAddressTime = configDto.getEmsAddressTime();
        int dateStoreTime = configDto.getDateStoreTime();
        int initialDelay = 5000; // 初始延迟时间，单位为毫秒
        int fengguTime = tacticsTime;
        int deleteToDBTime = deleteTime;
        int writeModBusFaultData2DBTime = faultStoreTime;
        int writerToEMSTime = emsAddressTime;
        int writeModBusRTUDataTime = dateStoreTime;
        taskScheduler.schedule(this::fenggu, new PeriodicTrigger(fengguTime, TimeUnit.MILLISECONDS) {{
            setInitialDelay(initialDelay);
        }});

        taskScheduler.schedule(this::deleteToDB, new PeriodicTrigger(deleteToDBTime, TimeUnit.MILLISECONDS) {{
            setInitialDelay(initialDelay);
        }});

        taskScheduler.schedule(this::writeModBusFaultData2DB, new PeriodicTrigger(writeModBusFaultData2DBTime, TimeUnit.MILLISECONDS) {{
            setInitialDelay(initialDelay);
        }});

        taskScheduler.schedule(this::writerToEMS, new PeriodicTrigger(writerToEMSTime, TimeUnit.MILLISECONDS) {{
            setInitialDelay(initialDelay);
        }});

        taskScheduler.schedule(this::writeModBusRTUData, new PeriodicTrigger(writeModBusRTUDataTime, TimeUnit.MILLISECONDS) {{
            setInitialDelay(initialDelay);
        }});
    }

    @Async("customizeThreadPool")
//    @Scheduled(fixedDelay = 2000)
    public void fenggu() {
//        if (GlobalParam.PVState == 1) {
//            System.out.println("光伏模式开始执行.....");
//            pvController.Spontaneous_pv();
//        } else if (GlobalParam.fgState == 1) {
//            System.out.println("峰谷开始运行.....");
//            peakValleyController.fengguCtl();
//        }else if (GlobalParam.backState == 1) {
//            System.out.println("备电开始运行.....");
//            backupController.backupMode();
//        }else
            if (GlobalParam.socState == 1){
            socRegionController.socRegionCtl();
        }
    }

    /**
     * 定时删除过期数据表
     */
    @Async("customizeThreadPool")
//    @Scheduled(fixedDelay = 72000000)
    public void deleteToDB(){
        try {
            DeleteOnTime.deleteOnTime();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }


    /**
     * 故障信息存储
     */
    @Async("customizeThreadPool")
//    @Scheduled(fixedDelay = 5000)
    public void writeModBusFaultData2DB() {
        modbusConfService.writeModBusFaultData2DB();
        System.out.println("故障信息存储");
    }

    /**
     * 给EMS地址赋值
     */
    @Async("customizeThreadPool")
//    @Scheduled(fixedDelay = 500)
    public void writerToEMS() {
        modbusConfService.writerToEMS();
    }

    /*
     * 历史数据存储
     * */
    @Async("customizeThreadPool")
//    @Scheduled(fixedDelay = 2000)
    public void writeModBusRTUData() {
        modbusConfService.writeModBusData2DB();
    }

}
